let ideModuleDir = "";
let workSpaceDir = process.argv[4].replace("\\.laya\\gulpfile.js", "");
const through = require('through2')
const gulp = require(ideModuleDir + "gulp");
const rollup = require(ideModuleDir + "rollup");
const typescript = require(ideModuleDir + 'rollup-plugin-typescript2');//typescript2 plugin
const glsl = require(ideModuleDir + 'rollup-plugin-glsl');
const path = require('path');
const fs = require('fs');
const del = require(ideModuleDir + "del");

// 如果是发布时调用编译功能，增加prevTasks
let prevTasks = "";
if (global.publish) {
    prevTasks = ["loadConfig"];
}

gulp.task("compile", function () {
    return rollup.rollup({
        input: workSpaceDir + '/src/Main.ts',
        onwarn: (waring, warn) => {
            if (waring.code == "CIRCULAR_DEPENDENCY") {
                console.log("warnning Circular dependency:");
                console.log(waring);
            }
        },
        treeshake: false, //建议忽略
        plugins: [
            typescript({
                tsconfig: workSpaceDir + "/tsconfig.json",
                check: true, //Set to false to avoid doing any diagnostic checks on the code
                tsconfigOverride: {compilerOptions: {removeComments: true, sourceMap: true}},
                include: /.*.ts/,
            }),
            glsl({
                // By default, everything gets included
                include: /.*(.glsl|.vs|.fs)$/,
                sourceMap: false,
                compress: false
            }),
        ]
    }).then(bundle => {
        return bundle.write({
            file: workSpaceDir + '/bin/js/bundle.js',
            format: 'iife',
            name: 'laya',
            sourcemap: true
        });
    }).catch(err => {
        console.log(err);
    });
});


// 清除bin/js文件
gulp.task("cleanBinJs", function (cb) {
    let delList = [`${workSpaceDir}/bin/js/**`, `${workSpaceDir}/.rpt2_cache/**`];
    del(delList, {force: true}).then(paths => {
        cb();
    }).catch((err) => {
        throw err;
    })
});


gulp.task("clean", gulp.series("cleanBinJs"), function () {
    return rollup.rollup({
        input: workSpaceDir + '/src/Main.ts',
        onwarn: (waring, warn) => {
            if (waring.code == "CIRCULAR_DEPENDENCY") {
                console.log("warnning Circular dependency:");
                console.log(waring);
            }
        },
        treeshake: false, //建议忽略
        plugins: [
            typescript({
                tsconfig: workSpaceDir + "/tsconfig.json",
                check: true, //Set to false to avoid doing any diagnostic checks on the code
                tsconfigOverride: {compilerOptions: {removeComments: true}},
                clean: true,
                include: /.*.ts/,
            }),
            glsl({
                // By default, everything gets included
                include: /.*(.glsl|.vs|.fs)$/,
                sourceMap: false,
                compress: false
            }),
        ]
    }).then(bundle => {
        return bundle.write({
            file: workSpaceDir + '/bin/js/bundle.js',
            format: 'iife',
            name: 'laya',
            sourcemap: true
        });
    }).catch(err => {
        console.log(err);
    });
});



//图片编码
let binPath = path.join(workSpaceDir, "bin").replace(/\\/g, "/");
let imgReg = binPath + "/**/*.{png,jpg}";
//排除不加密图片
let excludeReg = "!" + binPath + "/{logo.png,loadbg.jpg}";


